package com.example.booksdeal.basic;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;

import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

import com.example.booksdeal.R;
import com.example.booksdeal.adapter.FragmentAdapter;
import com.example.booksdeal.fragment.Book1Fragment;
import com.example.booksdeal.fragment.Book2Fragment;
import com.example.booksdeal.fragment.Book3Fragment;
import com.example.booksdeal.fragment.Book4Fragment;
import com.example.booksdeal.util.DBOpenHelper;

import java.util.ArrayList;
import java.util.HashMap;

public class BooksListActivity extends AppCompatActivity {

    private DBOpenHelper dbOpenHelper;  //声明DBOpenHelper对象

    private Book1Fragment book1Fragment;
    private Book2Fragment book2Fragment;
    private Book3Fragment book3Fragment;
    private Book4Fragment book4Fragment;
    private LinearLayout moren,shijian,jiagedi,jiagegao;
    private OnClick myclick;
    private TextView moren_text,shijian_text,jiagedi_text,jiagegao_text;
    //定义颜色值
    private int Black = 0xFF000000;
    private int Orange =0xFFFF6600;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_books_list);
        Bundle bundle = getIntent().getExtras();
        myclick = new OnClick();
        initview();
    }

    private class OnClick implements View.OnClickListener{

        @Override
        public void onClick(View view) {
            switch (view.getId()){
                case R.id.moren:
                    if (book1Fragment == null){
                        book1Fragment = new Book1Fragment();
                    }
                    clearChioce();
                    checked(view.getId());
                    getSupportFragmentManager().beginTransaction().replace(R.id.bookfragment,book1Fragment).commitAllowingStateLoss();
                    break;
                case R.id.shijian:
                    if (book2Fragment == null){
                        book2Fragment = new Book2Fragment();
                    }
                    clearChioce();
                    checked(view.getId());
                    getSupportFragmentManager().beginTransaction().replace(R.id.bookfragment,book2Fragment).commitAllowingStateLoss();
                    break;
                case R.id.jiagedi:
                    if (book3Fragment == null){
                        book3Fragment = new Book3Fragment();
                    }
                    clearChioce();
                    checked(view.getId());
                    getSupportFragmentManager().beginTransaction().replace(R.id.bookfragment,book3Fragment).commitAllowingStateLoss();
                    break;
                case R.id.jiagegao:
                    if (book4Fragment == null){
                        book4Fragment = new Book4Fragment();
                    }
                    clearChioce();
                    checked(view.getId());
                    getSupportFragmentManager().beginTransaction().replace(R.id.bookfragment,book4Fragment).commitAllowingStateLoss();
                    break;
            }
        }
    }

    private void initview() {
        //实例化Book1Fragment
        book1Fragment = new Book1Fragment();
        //将book1Fragment添加到Activity中,初始化第一个Fragment
        getSupportFragmentManager().beginTransaction().add(R.id.bookfragment,book1Fragment).commitAllowingStateLoss();
        moren = findViewById(R.id.moren);
        shijian = findViewById(R.id.shijian);
        jiagedi = findViewById(R.id.jiagedi);
        jiagegao = findViewById(R.id.jiagegao);
        moren_text = findViewById(R.id.moren_text);
        shijian_text = findViewById(R.id.shijian_text);
        jiagedi_text = findViewById(R.id.jiagedi_text);
        jiagegao_text = findViewById(R.id.jiagegao_text);
        moren.setOnClickListener(myclick);
        shijian.setOnClickListener(myclick);
        jiagedi.setOnClickListener(myclick);
        jiagegao.setOnClickListener(myclick);
    }

    //建立一个清空选中状态的方法
    private void clearChioce() {
        moren_text.setTextColor(Black);
        shijian_text.setTextColor(Black);
        jiagedi_text.setTextColor(Black);
        jiagegao_text.setTextColor(Black);
        moren_text.setTextSize(16);
        shijian_text.setTextSize(16);
        jiagedi_text.setTextSize(16);
        jiagegao_text.setTextSize(16);
    }

    //更新选中状态的方法
    private void checked(int num) {
        switch (num) {
            case R.id.moren:
                moren_text.setTextColor(Orange);
                moren_text.setTextSize(20);
                break;
            case R.id.shijian:
                shijian_text.setTextColor(Orange);
                shijian_text.setTextSize(20);
                break;
            case R.id.jiagedi:
                jiagedi_text.setTextColor(Orange);
                jiagedi_text.setTextSize(20);
                break;
            case R.id.jiagegao:
                jiagegao_text.setTextColor(Orange);
                jiagegao_text.setTextSize(20);
                break;
        }

    }
}
